//
//  PersonalHeadView.m
//  项目三
//
//  Created by rimi1 on 2016/12/9.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "PersonalHeadView.h"

static NSInteger spacing_ = 20;

@implementation PersonalHeadView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.bgImage];
        [self addSubview:self.headImage];
        [self addSubview:self.name];
        [self addSubview:self.line];
        [self addSubview:self.fans];
        [self addSubview:self.concern];
        [self addSubview:self.myMenu];
        [self addSubview:self.myCollect];
    }
    return self;
}

- (void)fcAssignmentWithFansCount:(NSString *)fCount concernCount:(NSString *)cCount {
    CGFloat fWidth = [self widthWithText:fCount];
    
    CGRect rect = self.fans.frame;
    rect.size.width = WXQAdaptionWidth(40) + fWidth;
    rect.origin.x = CGRectGetMinX(self.line.frame) - 5 - rect.size.width;
    self.fans.frame = rect;
    
    CGRect fCountRect = self.fans.count.frame;
    fCountRect.size.width = fWidth;
    
    self.fans.count.frame = fCountRect;
    self.fans.count.text = fCount;
    
    CGFloat cWidth = [self widthWithText:cCount];
    
    CGRect cCountRect = self.concern.count.frame;
    cCountRect.size.width = cWidth;
    self.concern.count.frame = cCountRect;
    
    self.concern.count.text = cCount;
}

- (CGFloat)widthWithText:(NSString *)text {
    CGFloat width = [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, self.fans.bounds.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.fans.title.font} context:nil].size.width;
    
    return width;
}

- (void)signInWithIs:(BOOL)is {
    // 真为登录
    if (is) {
        [self insertSubview:self.bgImage atIndex:0];
        [self.signIn removeFromSuperview];
    }else {
        [self addSubview:self.bgImage];
        [self addSubview:self.signIn];
    }
}

#pragma mark - respodnsTo..

- (void)respondsToHead:(UITapGestureRecognizer *)gesture {
    if (self.tapHead) {
        self.tapHead();
    }
}

- (void)respondsToButton:(UIButton *)sender {
    // 当时选中状态，或者不是登录状态
    if (sender.selected) {
        return;
    }
    
    if (self.tapButton) {
        self.tapButton(sender);
    }
}

- (void)respondsToSignIn:(UIButton *)sender {
    if (self.tapSignIn) {
        self.tapSignIn(sender);
    }
}

#pragma mark - get

- (UIImageView *)bgImage {
    
    if (!_bgImage) {
        
        CGFloat y = -WXQAdaptionHeight(180);
        CGFloat height = WXQAdaptionHeight(180) - y;
        _bgImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, y, kWIDTH, height)];
        _bgImage.backgroundColor = kCOLOR_RGB(80, 80, 80, 1);
        
    }
    return _bgImage;
}

- (UIImageView *)headImage {
    
    if (!_headImage) {
        
        CGFloat diameter = WXQAdaptionHeight(80);
        _headImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, WXQAdaptionHeight(spacing_), diameter, diameter)];
        CGPoint point = _headImage.center;
        point.x = kWIDTH/2;
        _headImage.center = point;
        _headImage.layer.cornerRadius = _headImage.bounds.size.width/2;
        _headImage.layer.masksToBounds = YES;
        _headImage.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(respondsToHead:)];
        [_headImage addGestureRecognizer:tap];
        
    }
    return _headImage;
}

- (UILabel *)name {
    
    if (!_name) {
        
        CGFloat height = WXQAdaptionHeight(18);
        _name = [[UILabel alloc] initWithFrame:CGRectMake(spacing_, CGRectGetMaxY(self.headImage.frame) + WXQAdaptionHeight(spacing_/2), kWIDTH - spacing_ * 2, height)];
        _name.font = [UIFont systemFontOfSize:height];
        _name.textAlignment = NSTextAlignmentCenter;
        _name.text = @"名字";
        _name.textColor = [UIColor whiteColor];
        
    }
    return _name;
}

- (UIView *)line {
    
    if (!_line) {
        
        _line = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.name.frame) + WXQAdaptionHeight(spacing_/2), 1, WXQAdaptionHeight(20))];
        _line.backgroundColor = [UIColor whiteColor];
        CGPoint point = _line.center;
        point.x = kWIDTH/2;
        _line.center = point;
        
    }
    return _line;
}

- (PesonalButton *)fans {
    
    if (!_fans) {
        
        CGFloat height = WXQAdaptionHeight(18);
        _fans = [[PesonalButton alloc] initWithFrame:CGRectMake(0, CGRectGetMinY(self.line.frame), WXQAdaptionWidth(40), height)];
        _fans.title.text = @"粉丝";
        [_fans addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
        _fans.tag = 100;
        
    }
    return _fans;
}

- (PesonalButton *)concern {
    
    if (!_concern) {
        
        CGFloat x = CGRectGetMaxX(self.line.frame);
        _concern = [[PesonalButton alloc] initWithFrame:CGRectMake(x + 5, CGRectGetMinY(self.fans.frame), kWIDTH - x - spacing_, CGRectGetHeight(self.fans.bounds))];
        _concern.title.text = @"关注";
        [_concern addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
        _concern.tag = 101;
        
    }
    return _concern;
}

- (UIButton *)myMenu {
    
    if (!_myMenu) {
        
        _myMenu = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.bgImage.frame), kWIDTH/2, WXQAdaptionHeight(30))];
        [_myMenu setTitle:@"菜谱" forState:UIControlStateNormal];
        [_myMenu setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_myMenu setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        _myMenu.selected = YES;
        _myMenu.backgroundColor = kCOLOR_RGB(251, 251, 251, 1);
        [_myMenu addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
        _myMenu.tag = 102;
        
    }
    return _myMenu;
}

- (UIButton *)myCollect {
    
    if (!_myCollect) {
        
        _myCollect = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.myMenu.frame), CGRectGetMinY(self.myMenu.frame), CGRectGetWidth(self.myMenu.bounds), CGRectGetHeight(self.myMenu.bounds))];
        [_myCollect setTitle:@"收藏" forState:UIControlStateNormal];
        [_myCollect setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_myCollect setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        _myCollect.backgroundColor = _myMenu.backgroundColor;
        [_myCollect addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
        _myCollect.tag = 103;
        
    }
    return _myCollect;
}

- (UIButton *)signIn {
    
    if (!_signIn) {
        
        _signIn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, WXQAdaptionWidth(80), 44)];
        CGPoint point = _signIn.center;
        point.x = self.bounds.size.width/2;
        point.y = self.bounds.size.height/2;
        _signIn.center = point;
        _signIn.backgroundColor = [UIColor orangeColor];
        [_signIn setTitle:@"登 录" forState:UIControlStateNormal];
        _signIn.layer.cornerRadius = 5;
        [_signIn addTarget:self action:@selector(respondsToSignIn:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _signIn;
}

@end












